package com.yxq.mdt.app.modules.video.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bigkoo.convenientbanner.utils.ScreenUtil;
import com.blankj.utilcode.util.ToastUtils;
import com.orhanobut.hawk.Hawk;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.wuhenzhizao.titlebar.widget.CommonTitleBar;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.base.BaseActivity;
import com.yxq.mdt.app.constant.Constant;
import com.yxq.mdt.app.constant.SPKey;
import com.yxq.mdt.app.dialog.CostumerDialog;
import com.yxq.mdt.app.modules.personal.bean.MyPersonalBean;
import com.yxq.mdt.app.modules.personal.ui.activity.RechargeActivity;
import com.yxq.mdt.app.modules.socialcontact.ui.activity.SocialPersonalHomepageAc;
import com.yxq.mdt.app.modules.socialcontact.ui.activity.VideoInviteChatActivity;
import com.yxq.mdt.app.modules.socialcontact.ui.activity.VoiceInviteChatActivity;
import com.yxq.mdt.app.modules.video.adapter.ChatClassifyDataAdapter;
import com.yxq.mdt.app.modules.video.bean.AdvertBean;
import com.yxq.mdt.app.modules.video.bean.ChannelListBean;
import com.yxq.mdt.app.modules.video.bean.MLUserList;
import com.yxq.mdt.app.modules.video.bean.VideoNearbyBean;
import com.yxq.mdt.app.modules.video.bean.VideoNearbyListBean;
import com.yxq.mdt.app.modules.video.model.MainVideoModel;
import com.yxq.mdt.app.modules.video.presenter.IMainVideoContract;
import com.yxq.mdt.app.modules.video.presenter.MainVideoPresenter;
import com.yxq.mdt.app.utils.GridSpacingItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import static com.blankj.utilcode.util.ActivityUtils.startActivity;

/**
 * 密聊分类详情页
 */
public class ChatClassifyDataAc extends BaseActivity<MainVideoPresenter> implements IMainVideoContract.View {
    @BindView(R.id.titlebar)
    CommonTitleBar commonTitleBar;
    @BindView(R.id.class_chat_recycler)
    RecyclerView classChatRecycler;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    ChatClassifyDataAdapter chatClassifyDataAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_chat_classify_data;
    }

    @Override
    public void initView() {
        mPresenter = new MainVideoPresenter(this, new MainVideoModel());
        String title = getIntent().getStringExtra("Text");
        commonTitleBar.getCenterTextView().setText(title);
        classChatRecycler.setLayoutManager(new GridLayoutManager(mActivity, 2));
        chatClassifyDataAdapter = new ChatClassifyDataAdapter(R.layout.item_chat_list, null);
        classChatRecycler.setAdapter(chatClassifyDataAdapter);
        if (classChatRecycler.getItemDecorationCount() == 0) {
            classChatRecycler.addItemDecoration(new GridSpacingItemDecoration(2, ScreenUtil.dip2px(mActivity, 15), true));
        }
        chatClassifyDataAdapter.setOnItemClickListener((adapter, view, position) -> {
            Intent intent = new Intent(mActivity, SocialPersonalHomepageAc.class);
            intent.putExtra("userId", chatClassifyDataAdapter.getItem(position).getUserId());
            startActivity(intent);
        });
        chatClassifyDataAdapter.setOnItemChildClickListener((adapter1, view, position) -> {
            switch (view.getId()) {
                case R.id.open_chat_lint_one:
                    Intent intent = new Intent(mActivity, SocialPersonalHomepageAc.class);
                    intent.putExtra("userId", chatClassifyDataAdapter.getItem(position).getUserId());
                    startActivity(intent);
                    break;
                case R.id.is_phone_layout:
                    String localUserId = "";
                    MyPersonalBean personalBean = Hawk.get(SPKey.SP_KEY_USER_MY_INFO);
                    if (personalBean != null) {
                        localUserId = personalBean.getUserId() + "";
                    }
                    String userName = "yxq" + chatClassifyDataAdapter.getItem(position).getUserId();
                    if (userName.equals("yxq" + localUserId)) {
                        ToastUtils.showShort("不能和自己通话");
                        return;
                    }
                    if (Constant.getSocialUser().getMoney() < chatClassifyDataAdapter.getItem(position).getPrice()) {

                        CostumerDialog dialog = new CostumerDialog.Builder(mActivity, R.style.custom_dialog_no_match)
                                .setContentView(R.layout.dialog_clear_cache_layout)
                                .setCancelable(true)
                                .create();

                        TextView tvTitle = dialog.findViewById(R.id.tv_title);

                        tvTitle.setText("余额不足");

                        TextView content = dialog.findViewById(R.id.tv_content);
                        content.setText("当前余额不足，充值后可继续送礼，是否去充值？");

                        //充值
                        TextView sure = dialog.findViewById(R.id.tv_sure);
                        sure.setText("充值");
                        sure.setOnClickListener(v -> {
                            Intent it = new Intent(mActivity, RechargeActivity.class);
                            startActivity(it);
                            dialog.dismiss();
                        });

                        //取消
                        TextView cancel = dialog.findViewById(R.id.tv_cancel);
                        cancel.setText("取消");
                        cancel.setOnClickListener(v -> {
                            dialog.dismiss();
                        });
                        dialog.show();
                        return;
                    }

                    if (chatClassifyDataAdapter.getItem(position).getAnInt() == 1) {
                        //单参数
                        Intent goToChat = new Intent(mActivity, VoiceInviteChatActivity.class);
                        goToChat.putExtra("nickname", "yxq" + userName);
                        startActivity(goToChat);
                    } else {
                        //跳转视频聊天界面
                        Intent goToChat = new Intent(mActivity, VideoInviteChatActivity.class);
                        goToChat.putExtra("nickname", "yxq" + userName);
                        startActivity(goToChat);
                    }

                    break;
                default:
            }
        });
        refreshLayout.setOnRefreshListener(refreshLayout -> {
            page = 1;
            mPresenter.getMlList(page, 20, 1, getIntent().getIntExtra("id", -1) + "", null, null);
        });
        refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            page++;
            mPresenter.getMlList(page, 20, 1, getIntent().getIntExtra("id", -1) + "", null, null);
        });
        chatClassifyDataAdapter.setEmptyView(R.layout.layout_empty_view, classChatRecycler);
        chatClassifyDataAdapter.isUseEmpty(false);
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        refreshLayout.autoRefresh();
    }

    @Override
    public void onChannelListSuccess(List<ChannelListBean> channelListBeanList) {

    }

    @Override
    public void onChannelListFailed(String error) {

    }

    @Override
    public void onUserListSuccess(MLUserList userList) {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
        List<VideoNearbyListBean> videoNearbyListBeanList = new ArrayList<>();
        for (MLUserList.ListBean u : userList.getList()) {
            String price = u.getSoundsVideoStatus() == 1 ? (u.getGsLiveGrade().getVoicePrice() + "豆/分钟") : (u.getGsLiveGrade().getVideoPrice() + "豆/分钟");

            int voiceLen = 0;
            String voicePath = null;
            if (u.getVoiceFile() != null) {
                voiceLen = u.getVoiceFile().getFileLength();
                voicePath = u.getVoiceFile().getFilePath();
            }

            VideoNearbyListBean videoChatListBean = new VideoNearbyListBean(price, voiceLen, u.getHeadimg(), u.getResidence(), u.getHeadimg(), u.getGeneration(), u.getAge() + "", u.getGsName(), u.getSoundsVideoStatus(), u.getVideoDetails(), String.valueOf(u.getSex()), u.getHeadimg(), voicePath, u.getDistance());
            videoChatListBean.setPrice(u.getSoundsVideoStatus() == 1 ? (u.getGsLiveGrade().getVoicePrice()) : (u.getGsLiveGrade().getVideoPrice()));
            videoChatListBean.setUserId(u.getUserId());
            videoNearbyListBeanList.add(videoChatListBean);
        }

        if (page == 1) {
            chatClassifyDataAdapter.setNewData(videoNearbyListBeanList);
        } else {
            if (userList.getList() != null && userList.getList().size() != 0) {
                chatClassifyDataAdapter.addData(videoNearbyListBeanList);
            } else {
                refreshLayout.finishLoadMoreWithNoMoreData();
            }
        }

        chatClassifyDataAdapter.isUseEmpty(!(chatClassifyDataAdapter.getData().size() > 0));
    }

    @Override
    public void onUserListFailed(String error) {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
    }

    @Override
    public void onAdvertSuccess(AdvertBean advertBean) {

    }

    @Override
    public void onAdvertSuccessFailed(String error) {

    }
}
